package com.room.room.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.utils.PageUtils;
import com.project.utils.Query;

import com.room.room.dao.HouseSecondaryDao;
import com.room.room.entity.HouseSecondaryEntity;
import com.room.room.service.HouseSecondaryService;


@Service("houseSecondaryService")
public class HouseSecondaryServiceImpl extends ServiceImpl<HouseSecondaryDao, HouseSecondaryEntity> implements HouseSecondaryService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String page1 = (String) params.get("page");
        //从redis中查询获取列表
        IPage<HouseSecondaryEntity> SecondList = (IPage<HouseSecondaryEntity>) redisTemplate.boundHashOps("HouseList").get("SecondList");

        //判断redis中是否有列表缓存并且是第一页
        if (SecondList==null && Integer.parseInt(page1)==1){//没有,从数据库查
            IPage<HouseSecondaryEntity> page = this.page(
                    new Query<HouseSecondaryEntity>().getPage(params),
                    new QueryWrapper<HouseSecondaryEntity>()
            );
            //将数据存入到redis
            redisTemplate.boundHashOps("HouseList").put("SecondList",page);
            System.out.println("数据库查");
            return new PageUtils(page);
        }else{//有,redis查
            System.out.println("redis查");
            return new PageUtils(SecondList);
        }

    }
}